package org.lxy.controller;


import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.api.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.lxy.domain.PointsLogEntity;
import org.lxy.domain.RecordLogEntity;
import org.lxy.domain.RecycleGoodsEntity;
import org.lxy.domain.UserInfoEntity;
import org.lxy.domain.constant.RocketTag;
import org.lxy.domain.constant.RocketTopic;
import org.lxy.domain.mq.ChangeReq;
import org.lxy.service.PointsLogService;
import org.lxy.service.RecordLogService;
import org.lxy.service.RecycleGoodsService;
import org.lxy.service.UserInfoService;
import org.lxy.utils.CoreUtils;
import org.lxy.utils.PageUtils;
import org.lxy.utils.RocketUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/recordlog")
@Slf4j
public class RecordLogController {
    @Autowired
    private RecordLogService recordLogService;
    @Autowired
    private RocketUtils rocketUtils;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public ModelAndView list(@RequestParam Map<String, Object> params){
        PageUtils page = recordLogService.queryPage(params);

        return null;
    }


    @RequestMapping("change")
    public void change(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=UTF-8");
        String id = request.getParameter("id");
        UserInfoEntity userinfo =(UserInfoEntity) request.getSession().getAttribute("Account");
        PrintWriter out = response.getWriter();
        if(userinfo == null){
            out.write("0");
            return;
        }
        RecycleGoodsService recycleGoodsService = CoreUtils.recycleGoodsService;
        RecycleGoodsEntity entity = recycleGoodsService.getById(Integer.parseInt(id));
        if(entity.getChangePoints()>userinfo.getPoints()){
            out.write("2");
            return;

        }
        SendResult send = rocketUtils.send(RocketTopic.RECYCLING_GOODS_COMMON.getName()
                , Arrays.asList(RocketTag.SAVE_GOODS, RocketTag.SAVE_POINTSLOG, RocketTag.SAVE_RECORDLOG).stream().collect(Collectors.joining(","))
                , JSONObject.toJSONString(ChangeReq.builder().goodsId(Integer.parseInt(id)).userId(userinfo.getId()).build()));
        log.info("mq兑换消息发送成功! [{}]",send);
        out.write("1");
    }


   /* *//**
     * 信息
     *//*
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		RecordLogEntity recordLog = recordLogService.getById(id);

        return R.ok().put("recordLog", recordLog);
    }

    *//**
     * 保存
     *//*
    @RequestMapping("/save")
    public R save(@RequestBody RecordLogEntity recordLog){
		recordLogService.save(recordLog);

        return R.ok();
    }

    *//**
     * 修改
     *//*
    @RequestMapping("/update")
    public R update(@RequestBody RecordLogEntity recordLog){
		recordLogService.updateById(recordLog);

        return R.ok();
    }

    *//**
     * 删除
     *//*
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		recordLogService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }*/

}
